package com.oracle.lingshangejia.adapter;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.bean.ResidentBean;

import java.util.List;

public class ResidentListAdapter extends BaseQuickAdapter<ResidentBean.DataBean, BaseViewHolder>/*RecyclerView.Adapter<ResidentListAdapter.VH> */ {
    public ResidentListAdapter(@Nullable List<ResidentBean.DataBean> data) {
        super(R.layout.item_reservationsuccess, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ResidentBean.DataBean item) {
        helper.setText(R.id.tv_residentfile_name, item.getXm());
        if (item.getXb() == 2) {
            helper.setText(R.id.tv_sex_age, "女");
        } else {
            helper.setText(R.id.tv_sex_age, "男");
        }

        RequestOptions options = new RequestOptions().circleCrop()
            .placeholder(R.drawable.ic_info_user)
            .error(R.drawable.ic_info_user)
            .fallback(R.drawable.ic_info_user);

        Glide.with(mContext)
            .load(Constant.imgurl + item.getTxUrl())
            .apply(options)
            .into((ImageView) helper.getView(R.id.iv_residentfile_img));
        helper.getView(R.id.ll_call).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + item.getBrdh());
                intent.setData(data);
                if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                mContext.startActivity(intent);

            }
        });
    }
/*
    public ResidentListAdapter() {
    }

    @Override
    public void onBindViewHolder(@NonNull ResidentListAdapter.VH vh, int i) {

    }

    @Override
    public int getItemCount() {
        return 5;
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        //LayoutInflater.from指定写法
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_resident, parent, false);
        return new VH(v);
    }
    //② 创建ViewHolder
    public static class VH extends RecyclerView.ViewHolder{
        public final TextView title;
        public VH(View v) {
            super(v);
            title = (TextView) v.findViewById(R.id.title);
        }
    }*/
}
